package com.hzlx.boms.controller;

import java.util.List;
import java.io.IOException;

import com.hzlx.BomsDto;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.hzlx.boms.domain.Boms;
import com.hzlx.boms.service.IBomsService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
import java.util.stream.Collectors;


/**
 * BOM清单Controller
 * 
 * @author ruoyi
 * @date 2025-07-01
 */
@RestController
@RequestMapping("/base")
public class BomsController extends BaseController
{
    @Autowired
    private IBomsService bomsService;

    /**
     * 查询BOM清单列表
     */
    @RequiresPermissions("base-data-service:boms:list")
    @GetMapping("/boms/list")
    public TableDataInfo list(Boms boms)
    {
        startPage();
        List<Boms> list = bomsService.selectBomsList(boms);
        return getDataTable(list);
    }
    @GetMapping("/bomsDto/list")//订单 产品 物料查询
    public List<BomsDto> list(BomsDto bomsDto)
    {
        Boms boms = new Boms();
        // 复制属性
        BeanUtils.copyProperties(bomsDto, boms);
        startPage();
        List<Boms> list = bomsService.selectBomsListByProductId(boms);
        List<BomsDto> dtoList = list.stream()
                .map(entity -> {
                    BomsDto dto = new BomsDto();
                    dto.setBomId(entity.getBomId());
                    // 其他属性...
                    return dto;
                })
                .toList();
        return dtoList;
    }
    /**
     * 导出BOM清单列表
     */
    @RequiresPermissions("base-data-service:boms:export")
    @Log(title = "BOM清单", businessType = BusinessType.EXPORT)
    @PostMapping("/boms/export")
    public void export(HttpServletResponse response, Boms boms)
    {
        List<Boms> list = bomsService.selectBomsList(boms);
        ExcelUtil<Boms> util = new ExcelUtil<Boms>(Boms.class);
        util.exportExcel(response, list, "BOM清单数据");
    }

    /**
     * 获取BOM清单详细信息
     */
    @RequiresPermissions("base-data-service:boms:query")
    @GetMapping(value = "/boms/{bomId}")
    public AjaxResult getInfo(@PathVariable("bomId") Long bomId)
    {
        return success(bomsService.selectBomsByBomId(bomId));
    }

    /**
     * 新增BOM清单
     */
    @RequiresPermissions("base-data-service:boms:add")
    @Log(title = "BOM清单", businessType = BusinessType.INSERT)
    @PostMapping("/boms")
    public AjaxResult add(@RequestBody Boms boms)
    {
        return toAjax(bomsService.insertBoms(boms));
    }

    /**
     * 修改BOM清单
     */
    @RequiresPermissions("base-data-service:boms:edit")
    @Log(title = "BOM清单", businessType = BusinessType.UPDATE)
    @PutMapping("/boms")
    public AjaxResult edit(@RequestBody Boms boms)
    {
        return toAjax(bomsService.updateBoms(boms));
    }

    /**
     * 删除BOM清单
     */
    @RequiresPermissions("base-data-service:boms:remove")
    @Log(title = "BOM清单", businessType = BusinessType.DELETE)
	@DeleteMapping("/boms/{bomIds}")
    public AjaxResult remove(@PathVariable Long[] bomIds)
    {
        return toAjax(bomsService.deleteBomsByBomIds(bomIds));
    }


    //根据productId查单行数据
    @GetMapping("/boms/productId/{productId}")
    public Boms queryByProductId(@PathVariable Long productId
    )
    {
        return bomsService.selectByProductId(productId);
    }
}
